package edu.zjut.hjg.saas.web.controller.home;

import static edu.zjut.hjg.saas.web.support.SaaSConstants.DEFAULT_CITY_CODE;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import edu.zjut.hjg.saas.biz.model.City;
import edu.zjut.hjg.saas.biz.model.Kindergarten;
import edu.zjut.hjg.saas.biz.model.Province;
import edu.zjut.hjg.saas.biz.service.KindergartenService;
import edu.zjut.hjg.saas.biz.service.LocationService;
import edu.zjut.hjg.saas.biz.support.Paginated;
import edu.zjut.hjg.saas.common.util.ParameterUtil;

@Controller
@RequestMapping("/home/search")
public class SearchController {

    private static final int    PAGE_SIZE = 15;

    @Value("${saas.web.app.server.port}")
    private int                 appServerPort;

    @Autowired
    private LocationService     locationService;

    @Autowired
    private KindergartenService kindergartenService;

    @ModelAttribute("provinces")
    public List<Province> prepareProvinces() {
        // 数据准备
        return locationService.listAllProvinces();
    }

    @ModelAttribute("port")
    public String prepareAppServerPort() {
        // 数据准备
        return appServerPort == 80 ? "" : ":" + appServerPort;
    }

    @RequestMapping("/byProvince.htm")
    public String byProvince(
                             @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                             String provinceCode, ModelMap model) {
        if (page < 1) {
            page = 1;
        }
        if (!ParameterUtil.notBlank(provinceCode)) {
            model.remove("provinces");
            model.remove("port");
            return "forward:byCity.htm";
        }
        model.addAttribute("cities", locationService.listCitesByProvinceCode(provinceCode));
        model.addAttribute("paginated", kindergartenService.searchByProvinceCode(provinceCode,
                PAGE_SIZE, page));
        model.addAttribute("queryUri", "/home/search/byProvince.htm?provinceCode=" + provinceCode);
        model.addAttribute("provinceCode", provinceCode);
        return "home/search/byLocation";
    }

    @RequestMapping("/byCity.htm")
    public String byCity(
                         @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                         String cityCode, Model model) {
        if (page < 1) {
            page = 1;
        }
        if (!ParameterUtil.notBlank(cityCode)) {
            cityCode = DEFAULT_CITY_CODE;
        }
        City city = locationService.findCityByCityCode(cityCode);
        if (city == null) {
            city = locationService.findCityByCityCode(DEFAULT_CITY_CODE);
        }
        model.addAttribute("cities", locationService.listCitesByProvinceCode(city.getProvince()
                .getCode()));
        model.addAttribute("paginated", kindergartenService.searchByCityCode(cityCode, PAGE_SIZE,
                page));
        model.addAttribute("queryUri", "/home/search/byCity.htm?cityCode=" + city.getCode());
        model.addAttribute("provinceCode", city.getProvince().getCode());
        model.addAttribute("cityCode", city.getCode());
        return "home/search/byLocation";
    }

    @RequestMapping("/byName.htm")
    public void byName(
                       @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                       String name, Model model) {
        if (page < 1) {
            page = 1;
        }
        model.addAttribute("name", name);
        if (ParameterUtil.notBlank(name)) {
            Paginated<Kindergarten> paginated = kindergartenService.searchByName(name, PAGE_SIZE,
                    page);
            model.addAttribute("paginated", paginated);
        } else {
            model.addAttribute("paginated", new Paginated<Kindergarten>());
        }
    }
}
